package com.gui.registration.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gui.hospital.feign.DoctorFeign;
import com.gui.hospital.feign.HospitalDepartmentFeign;
import com.gui.hospital.feign.HospitalFeign;
import com.gui.hospital.feign.NumberSourceFeign;
import com.gui.hospital.model.Hospital;
import com.gui.registration.mapper.RegistrationMapper;
import com.gui.registration.model.RegistrationRecord;
import com.gui.registration.service.RegistrationService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 言叶
 * Date：2025/9/3
 */
@Service
@AllArgsConstructor
public class RegistrationServiceImpl
        extends ServiceImpl<RegistrationMapper, RegistrationRecord>
        implements RegistrationService {
    private final HospitalFeign hospitalFeign;
    private final DoctorFeign doctorFeign;
    private final HospitalDepartmentFeign hospitalDepartmentFeign;
    private final NumberSourceFeign numberSourceFeign;
    private final RegistrationMapper registrationMapper;
    @Override
    public List<Hospital> getHospitalByName(String name) {
        //调用HospitalService
        return hospitalFeign.list(name);
    }

    @Override
    @GlobalTransactional
    public Boolean addRegistration(RegistrationRecord registration) {
        //添加挂号信息
        //补全挂号信息
        registration.setHospitalName(hospitalFeign.getHospitalById(registration.getHospitalId()).getChineseName());
        registration.setHdName(hospitalDepartmentFeign.getHospitalDepartmentById(registration.getHdId()).getName());
        registration.setDoctorName(doctorFeign.getById(registration.getDoctor()).getName());
        //设置订单ID
        registration.setOrderId(IdWorker.getId()+"");
        registrationMapper.insert(registration);
        //修改号源状态，调用医院模块
        numberSourceFeign.updateStatusById(registration.getNumberSourceId(), 1L);
        return true;
    }

    @Override
    public List<RegistrationRecord> getByUserId(Integer useId) {
        return registrationMapper.getByUserId(useId);
    }

    @Override
    @GlobalTransactional
    public Boolean cancelOrder(String orderId) {
        //1 逻辑删除订单  2 释放号源
        // 使用 Wrapper 条件构造器
        remove(new QueryWrapper<RegistrationRecord>().eq("order_id", orderId));
        //根据订单ID查询号源ID
        RegistrationRecord registrationRecord =
                registrationMapper.selectOne(new QueryWrapper<RegistrationRecord>().eq("order_id", orderId));
        numberSourceFeign.updateStatusById(registrationRecord.getNumberSourceId(), 0L);
        return true;
    }

    @Override
    @Transactional
    public boolean updateByOrderId(RegistrationRecord record) {
        QueryWrapper<RegistrationRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", record.getOrderId());
        return registrationMapper.update(record, queryWrapper) != 0;
    }
}
